# -*- coding: utf-8 -*-
# @Time : 2022-01-14 18:08
# @Author : Z
# @Function : 获取页面菜品数量

import time
from selenium.webdriver.common.by import By
from Common.Excel.excel import get_writer
from selenium import webdriver
from Common.Logger import path, logger

driver = webdriver.Chrome()
driver.get('http://172.30.2.247/ChfoodKitchen/pc/newDist/#/threeScreens')
ele = driver.find_element(By.XPATH, '//*[text()="菜品库存"]')
driver.execute_script('arguments[0].click()', ele)

# 获取菜品名称
x = []
time.sleep(1)
for i in range(1, 22):
    a = ('(//*[@class="col-12 text-left text-break"])' + '[' + str(i) + ']')
    goods = driver.find_element(By.XPATH, a)
    b = goods.get_attribute('innerText')
    x.append(b)
x.append('赠菜')
logger.info(x)

# 统计已完成菜品数量，写入Excel
driver.refresh()
writer = get_writer(path + '../excel/test.xlsx', path + '../excel/result_test.xlsx')
sheetname = writer.get_sheets()
writer.set_sheet(sheetname[0])
sum = 0
for i in range(1, len(x) + 1):
    writer.write(i, 0, x[i - 1])
    a = ('(//*[text()="' + x[i - 1] + '"])/../../div' + '[contains(text(),"已完成") ]')
    b = ('(//*[text()="' + x[i - 1] + '"])/../../div' + '[contains(text(),"退") ]')
    try:
        driver.implicitly_wait(0.1)
        a = driver.find_elements(By.XPATH, a)
        b = driver.find_elements(By.XPATH, b)
        writer.write(i, 1, str(len(a)))
        writer.write(i, 2, str(len(b)))
        logger.info((str(x[i - 1]) + ': ' + str(len(a))))
        sum = len(a) + sum
    except Exception as e:
        pass
writer.write(len(x) + 1, 0, '完成总量')
writer.write(len(x) + 1, 1, str(sum))
writer.save_close()
driver.quit()
